package com.shuyu.gsyvideoplayer.widget;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.ruihang.ijkplaylib.R;

import java.util.ArrayList;
import java.util.List;

public class BarrageListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<String> barrageList = new ArrayList<>();
    private Context ctx;

    public BarrageListAdapter(Context ctx,List<String> barrageList){
        this.ctx =ctx;
        this.barrageList = barrageList;
    }
    static class BarrageViewHolder extends RecyclerView.ViewHolder {
        TextView barrage;
        LinearLayout itemLayout;
        public BarrageViewHolder(View view) {
            super(view);
            itemLayout  =(LinearLayout)view.findViewById(R.id.item_layout);
            barrage = (TextView) view.findViewById(R.id.barrage_txt);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view = LayoutInflater.from(ctx).inflate(R.layout.barrage_list_view, parent, false);
        holder = new BarrageViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (null != barrageList && barrageList.size() > position) {
            String barrage = barrageList.get(position);
            BarrageViewHolder h = (BarrageViewHolder) holder;
            if(TextUtils.isEmpty(barrage)){
                h.itemLayout.setBackground(null);
                h.barrage.setText("");
            }else {
                h.itemLayout.setBackground(ctx.getResources().getDrawable(R.drawable.gen_living_comment_bg));
                String txa[] = barrage.split("=");
                barrage = barrage.replace("=", "");

                SpannableString spannableString = new SpannableString(barrage);
                ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#FF6D50"));
                spannableString.setSpan(colorSpan, 0, txa[0].length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                h.barrage.setText(spannableString);
            }
        }
    }

    public void addMessage(String msg){
        if(barrageList.size()>0&&TextUtils.isEmpty(barrageList.get(0))){
            barrageList.remove(0);
        }
        if(barrageList.size()>100){
            barrageList.remove(0);
        }
        barrageList.add(msg);
        notifyDataSetChanged();
    }
    public void setBarrageList(List<String> barrageList) {
        this.barrageList = barrageList;
    }

    public void clearData(){
        barrageList.clear();
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        if (null != barrageList) {
            return barrageList.size();
        }else {
            return 0;
        }
    }
}
